package com.hotelsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotelsystem.pojo.Floor;
import com.hotelsystem.pojo.Room;
import com.hotelsystem.pojo.RoomType;
import com.hotelsystem.pojo.Status;
import com.hotelsystem.pojo.vo.RoomQueryVo;
import com.hotelsystem.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/room")
public class RoomController {

    //对其进行日志操作
    @Resource(name="roomServiceImpl")
    private RoomService roomService;

    @Resource(name = "statusServiceImpl")
    private StatusService statusService;

    @Resource(name="floorServiceImpl")
    private FloorService floorService;

    @Resource(name = "roomTypeServiceImpl")
    private RoomTypeService roomTypeService;

    /**
     * 按条件查询房间信息
     * @param roomQueryVo 查询的对象
     * @param pageNum 当前页码
     * @param model
     * @param httpSession
     * @return
     */
    @RequestMapping("/toRoomSet")
    public String gotoRoomSet(RoomQueryVo roomQueryVo, Integer pageSize, @RequestParam("pageNum") Integer pageNum, Model model, HttpSession httpSession){

        System.out.println(roomQueryVo);
        try {

            //设置当前页和每页数量
            System.out.println("pageSize"+pageSize);
            if (pageSize == null){
                PageHelper.startPage(pageNum,5);
                List<Room> rooms = roomService.getPageforRoomLists(roomQueryVo);
                //获取PageInfo对象，该对象封装的分页的相关信息
                PageInfo<Room> pageInfo = new PageInfo<>(rooms);
                System.out.println(pageInfo.getList());
                model.addAttribute("pageInfo",pageInfo);
            } else if (pageSize==-1){
                PageHelper.startPage(pageNum,roomService.getPageforRoomLists(roomQueryVo).size());
                List<Room> rooms = roomService.getPageforRoomLists(roomQueryVo);
                //获取PageInfo对象，该对象封装的分页的相关信息
                PageInfo<Room> pageInfo = new PageInfo<>(rooms);
                model.addAttribute("pageInfo",pageInfo);
            }else {
                PageHelper.startPage(pageNum,pageSize);
                List<Room> rooms = roomService.getPageforRoomLists(roomQueryVo);
                //获取PageInfo对象，该对象封装的分页的相关信息
                PageInfo<Room> pageInfo = new PageInfo<>(rooms);
                model.addAttribute("pageInfo",pageInfo);
            }
            //客房类型
            List<RoomType> roomTypeLists = roomTypeService.getAllRoomType();
            //客房楼层
            List<Floor> floorLists = floorService.getAllFloor();

            //客房状态读取
            List<Status> statusLists = statusService.getAllStatus();
            model.addAttribute("roomTypeLists",roomTypeLists);
            model.addAttribute("floorLists",floorLists);
            model.addAttribute("statusLists",statusLists);
            //将绑定的数据换上
           // System.out.println("在这里测试是否传过来"+roomQueryVo.getFloor().getFloorId());
            model.addAttribute("pageSize",pageSize);
            model.addAttribute("roomQueryVo",roomQueryVo);

        } catch (Exception e) {

            e.printStackTrace();
        }
        return "room/roomSet/roomSet";
    }
    @RequestMapping("/toSeclectAll")
    @ResponseBody
    public Object SeclectAll(Room room,Model model){
        /*Map<String, Object> map = new HashMap<String, Object>();
        //设置limit
        map.put("start",pageBean.getFirstPage());
        map.put("size",pageBean.getPageSize());
        //查询所有的角色信息
        List<Room> lists = roomService.selectAll();
        //查询出总数量
        int totalCount = lists.size();
        //将总数量交给page对象进行分页操作
        pageBean.setTotalCount(totalCount);
        //调用查询用户列表的方法*/
        return null;
    }

    /**
     * 获取房间对象信息
     * @param roomId
     * @return
     */
    @RequestMapping("/getRoomInfo")
    @ResponseBody
    public Object getRoomInfoWithId(String roomId){
        try {
            return roomService.getRoomInfoWithId(roomId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除房间对象信息
     * @param roomId
     * @return
     */
    @RequestMapping("/toDeleteRoomInfo")
    @ResponseBody
    public Object deleteRoomInfoWithId(String roomId){
        Map<String,Object> maps = new HashMap<>();
        try {

             roomService.deleteRoomInfoWithId(roomId);
             maps.put("success",true);
        } catch (Exception e) {
            maps.put("success",false);
            e.printStackTrace();
        }
        return maps;
    }
    @RequestMapping("/addRoom")
    @ResponseBody
    public Object addRooomInfoByRoom(Room room,String imgBase64){
        Map<String,Object> maps = new HashMap<>();
        try {

            //添加用户信息
           roomService.addRoomInfoWithRoom(room,imgBase64);
            maps.put("success",true);
        } catch (Exception e) {
            maps.put("success",false);
            e.printStackTrace();
        }
        return  maps;
    }
    @ResponseBody
    @RequestMapping("/updateRoom")
    public  Object updateRoomInfoByRoom(Room room,String imgBase64,Integer oldRoomTypeId,Integer oldStatusId){
        Map<String,Object> maps = new HashMap<>();
        try {
            //System.out.println(room.getRoomId());
            //System.out.println("-"+imgBase64);
            //修改用户信息
            roomService.updateRoomInfoByRoom(room,imgBase64,oldRoomTypeId,oldStatusId);
            maps.put("success",true);
        } catch (Exception e) {
            maps.put("success",false);
            e.printStackTrace();
        }
        return  maps;
    };
}
